<template>
  <el-select
    v-model="value"
    :placeholder="props.placeholder"
    clearable
    filterable
    :multiple="props.multiple"
    collapse-tags
    @change="change"
  >
    <el-option
      v-for="item in overseasWarehouseOptions"
      :key="item.id"
      :label="item.warehouse_name"
      :value="item.id"
    />
  </el-select>
</template>
<script setup>
import useCommonStore from "@/store/modules/common";
const commonStore = useCommonStore();
const overseasWarehouseOptions = computed(() => {
  if (!commonStore.overseasWarehouseOptions) {
    commonStore.getOverseasWarehouseList();
  }
  return commonStore.overseasWarehouseOptions;
});
const props = defineProps({
  modelValue: [Number, Object, Array],
  placeholder: {
    type: String,
    default: "请选择",
  },
  multiple: {
    type: Boolean,
    default: false,
  },
});

const value = computed({
  get: () => {
    return props.modelValue;
  },
  set: (val) => {
    emit("update:modelValue", val);
  },
});

const emit = defineEmits(["update:modelValue", "change", "changeItem"]);
const change = (val) => {
  emit("update:modelValue", val);
  if (!props.multiple) {
    const item = overseasWarehouseOptions.value.find((i) => i.id == val);
    emit("change", item?.warehouse_name);
    emit("changeItem", item);
  }
};
</script>
